<?php

namespace App\Http\Controllers\Project;
use Illuminate\Database\Eloquent\SoftDeletestrait;
use Illuminate\Http\Request;

use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Base\BaseController;
use App\Models\User;
use App\Models\Project;

class ProjectController extends BaseController
{
	// 规定为空参数
	protected function rules($methodname)
    {
        switch ($methodname) {
            case 'projectList':
               	$params = ['token'=>'required'];
                break;
            case 'details':
                $params = ['project_id'=>'required|numeric',
                		   'token'=>'required'];
                break;
        }
        return $params;
    }
	//项目列表页
	function projectList(Request $request){
		$limit=10;
		$db=Project::select('*');
		if(!empty($request->input('title'))){
			$db->where('title', 'like', '%'.$request->input('title').'%');
		}
		if(!empty($request->input('project'))){
			$db->Where('group_id',$request->input('project'));
		}
		if(!empty($request->input('type'))){
			$db->Where('type',$request->input('type'));
		}
		if(!empty($request->input('city'))){
			$db->Where('city',$request->input('city'));
		}
		if(intval($request->input('limit'))){
			$limit=intval($request->input('limit'));
		}
		$page=intval($request->input('page'));
		if(empty($request->input('page'))){
			$page=0;
		}
		$offset=$page*$limit;
		$db->orderBy('created_at','desc');
		$db->offset($offset);
		$db->limit($limit);
		$projectList=$db->get();
		if(count($projectList)){
			return array('code' => config('constants.API_NOT_EXISTS_CODE'),'message' => $projectList);
		}else{
			return ['code' => config('constants.API_NULL_CODE'), 'message' => config('constants.API_NULL')];
		}
	}
	//项目详情页面
	function details(Request $request){
		$first=project::select('*')
    	->join('group', 'group.id', '=', 'project.group_id')
    	->where('project.id',$request->input('project_id'))
    	->first();
		if($first){
			return ['code' => config('constants.API_SUCCESS_CODE'), 'message' => $first];
		}else{
			return ['code' => config('constants.API_NULL_CODE'), 'message' => config('constants.API_NULL')];
		}
	}
	// 开启sql记录文件
	function startSql(){
		DB::connection()->enableQueryLog();
	}
	// 输出执行的sql语句
	function printSql(){
		$queries = DB::getQueryLog();
		$a = end($queries);
		$tmp = str_replace('?', '"'.'%s'.'"', $a["query"]);
		echo vsprintf($tmp, $a['bindings']);die;
	}
}
